package com.dotflat.satistics.repository.crm.impl;

import com.dotflat.satistics.cnst.CommonConstant;
import com.dotflat.satistics.repository.crm.BudgetRepository;
import com.dotflat.satistics.repository.crm.IncomeRepository;
import com.matt.foundation.constant.ContractConstant;
import com.matt.foundation.utils.DateUtil;
import com.matt.repository.base.BaseNativeSqlRepository;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Describle This Class Is
 * @Author ZengMin
 * @Date 2018/10/10 11:52
 */
@Repository
public class IncomeRepositoryImpl extends BaseNativeSqlRepository implements IncomeRepository {

    @Autowired
    private JdbcTemplate crmJdbcTemplate;

    @Override
    public Double findIncomeByAreaAndTime(String areaId, Date begin, Date end) {
        Double count = crmJdbcTemplate.queryForObject("select sum(con.practicalAmount) as count from contract as con where con.paidDate >=? and con.paidDate <= ? and con.payStatus=? and con.areaId=? and con.status!=?",
                new Object[]{begin,end,ContractConstant.Status.END.codeOf(), areaId, ContractConstant.Status.STOP.codeOf()},Double.class);
        return Objects.isNull(count) ? 0d : count;
    }

    @Override
    public Long findContratCount(String areaId, Date begin,Date end){
        Long result = crmJdbcTemplate.queryForObject(
                "SELECT count(1) as count from contract where payStatus = 1 and (`status` = 0 or `status` = 1)" +
                " and signDate>= ? and signDate <= ? and areaId = ?", new Object[]{begin,end, areaId}, Long.class);

        return result;
    }

    @Override
    public List<Map<String, Object>> findContratCount(Date begin,Date end,String areaIds) {
        List<Map<String, Object>> maps = crmJdbcTemplate.queryForList("SELECT count(1) as count,areaId from contract where payStatus = 1 and (`status` = 0 or `status` = 1)" +
                " and signDate>= ? and signDate <= ? and areaId in ("+areaIds+") GROUP BY areaId;", new Object[]{begin,end});
        return maps;
    }

    @Override
    public List<Map<String, Object>> findContratCountByArea(Date begin,Date end,String areaId) {
        List<Map<String, Object>> maps = crmJdbcTemplate.queryForList("SELECT count(1) as count,date(signDate) as created from contract where payStatus = 1 and" +
                " (`status` = 0 or `status` = 1) and signDate>= ? and signDate <= ? and areaId = "+areaId+" GROUP BY date(signDate);", new Object[]{begin,end});
        return maps;
    }

    @Override
    public Long findContratSignleCountByArea(Date begin,Date end,String areaId) {
        Long aLong = crmJdbcTemplate.queryForObject("SELECT count(1) as count from contract where payStatus = 1 and" +
                " (`status` = 0 or `status` = 1) and signDate>= ? and signDate <= ? and areaId = " + areaId , new Object[]{begin, end}, Long.class);
        return aLong == null ? 0L : aLong;
    }

}
